package jalus.service;

import jalus.model.Configure;
import jalus.model.PMF;

import java.util.ArrayList;
import java.util.List;

import javax.jdo.PersistenceManager;
import javax.jdo.Query;

import org.apache.commons.lang.math.NumberUtils;

import com.google.appengine.api.datastore.Text;


public class ConfigureService {
	/** 每页显示的文章数 **/
	
	private String blogTitle = "jalus";
	private String host = "http://127.0.0.1/";
	
	private int postnumperpage = 10;
	private int feedCount = 10;
	private int recentCommentCount = 10;
	private int recentGuestBookCount = 10;
	private int recentPostCount = 10;
	
	private String metaWeblogUserName = "metaweblog";
	private String metaWeblogPassword = "metaweblog";
	
	private String picasaUserName = "userName@gmail.com";
	private String picasaAlbumName = "picasaAlbumName";
	private String googleReaderUserName = "googleUserName";
	private String googleReaderPassword = "Password";
	private String doubanUserId = "doubanUserId";

	private String errorPage = "/error.jsp";
	
	/* 在<head></head>标记之间插入内容 */
	private String headText = " ";

	/* 在<body></body>标记之间插入内容 */
	private String bodyText = " ";
	/* 页面底部说明 */
	private String footerText = " ";
	/* 在post页文章之下，评论之上插入内容 */
	private String postText = " ";
	
	/* 如有多个域名，强制使用指定的域名，其他域名将重定向到指定域名 */
	private boolean uniqsite = false;
	
	private static ConfigureService instance = new ConfigureService();
	
	private ConfigureService(){
		load();
	}
	
	public static ConfigureService getInstance(){
		return instance;
	}
	
	/**
	 * 写到数据库
	 */
	public void save(){
		List<Configure> configureList = new ArrayList<Configure>();
		configureList.add(new Configure("postnumperpage", String.valueOf(this.postnumperpage)));
		configureList.add(new Configure("blogTitle", this.blogTitle));
		configureList.add(new Configure("recentCommentCount", String.valueOf(this.recentCommentCount)));
		configureList.add(new Configure("recentGuestBookCount", String.valueOf(this.recentGuestBookCount)));
		configureList.add(new Configure("feedCount", String.valueOf(this.feedCount)));
		configureList.add(new Configure("host", this.host));
		configureList.add(new Configure("recentPostCount", String.valueOf(this.recentPostCount)));
		
		configureList.add(new Configure("metaWeblogUserName", String.valueOf(this.metaWeblogUserName)));
		configureList.add(new Configure("metaWeblogPassword", String.valueOf(this.metaWeblogPassword)));
		configureList.add(new Configure("picasaUserName", this.picasaUserName));
		configureList.add(new Configure("Password", this.googleReaderPassword));
		configureList.add(new Configure("userName", this.googleReaderUserName));
		configureList.add(new Configure("picasaAlbumName", this.picasaAlbumName));
		configureList.add(new Configure("doubanUserId", this.doubanUserId));
		
		configureList.add(new Configure("errorPage", this.errorPage));
		configureList.add(new Configure("headText", new Text(this.headText)));
		configureList.add(new Configure("bodyText", new Text(this.bodyText)));
		configureList.add(new Configure("footerText", new Text(this.footerText)));
		configureList.add(new Configure("postText", new Text(this.postText)));
		PersistenceManager pm = PMF.get().getPersistenceManager();
		try{
			pm.makePersistentAll(configureList);
		}finally{
			pm.close();
		}
	}
	
	/**
	 * 从数据库中读取
	 */
	@SuppressWarnings("unchecked")
	public void load(){
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    Query query = pm.newQuery(Configure.class);
	    pm.setDetachAllOnCommit(true);
	    List<Configure> configureList;
	    try{
	    	configureList = (List<Configure>) query.execute();
	    }finally{
	    	query.closeAll();
	    }
	    if(configureList == null){
	    	return;
	    }
	    for(Configure configure : configureList){
	    	String key = configure.getKey();
	    	if(key.equals("postnumperpage")){
	    		this.postnumperpage = NumberUtils.toInt(configure.getValue());
	    	}else if(key.equals("blogTitle")){
	    		this.blogTitle = configure.getValue();
	    	}else if(key.equals("recentCommentCount")){
	    		this.recentCommentCount = NumberUtils.toInt(configure.getValue());
	    	}else if(key.equals("recentGuestBookCount")){
	    		this.recentGuestBookCount = NumberUtils.toInt(configure.getValue());
	    	}else if(key.equals("feedCount")){
	    		this.feedCount = NumberUtils.toInt(configure.getValue());
	    	}else if(key.equals("host")){
	    		this.host = configure.getValue();
	    	}else if(key.equals("recentPostCount")){
	    		this.recentPostCount = NumberUtils.toInt(configure.getValue());
	    	}else if(key.equals("metaWeblogUserName")){
	    		this.metaWeblogUserName = configure.getValue();
	    	}else if(key.equals("metaWeblogPassword")){
	    		this.metaWeblogPassword = configure.getValue();
	    	}else if(key.equals("picasaUserName")){
	    		this.picasaUserName = configure.getValue();
	    	}else if(key.equals("userName")){
	    		this.googleReaderUserName = configure.getValue();
	    	}else if(key.equals("Password")){
	    		this.googleReaderPassword = configure.getValue();
	    	}else if(key.equals("picasaAlbumName")){
	    		this.picasaAlbumName = configure.getValue();
	    	}else if(key.equals("errorPage")){
	    		this.errorPage = configure.getValue();
	    	}else if(key.equals("headText")){
	    		this.headText = configure.getTextValue().getValue();
	    	}else if(key.equals("bodyText")){
	    		this.bodyText = configure.getTextValue().getValue();
	    	}else if(key.equals("footerText")){
	    		this.footerText = configure.getTextValue().getValue();
	    	}else if(key.equals("postText")){
	    		this.postText = configure.getTextValue().getValue();
	    	}else if(key.equals("doubanUserId")){
	    		this.doubanUserId = configure.getValue();
	    	}
	    }
	}
	
	public int getPostnumperpage() {
		return postnumperpage;
	}

	public void setPostnumperpage(int postnumperpage) {
		this.postnumperpage = postnumperpage;
	}

	public String getBlogTitle() {
		return blogTitle;
	}

	public void setBlogTitle(String blogTitle) {
		this.blogTitle = blogTitle;
	}

	public int getRecentCommentCount() {
		return recentCommentCount;
	}

	public void setRecentCommentCount(int recentCommentCount) {
		this.recentCommentCount = recentCommentCount;
	}

	public int getRecentGuestBookCount() {
		return recentGuestBookCount;
	}

	public void setRecentGuestBookCount(int recentGuestBookCount) {
		this.recentGuestBookCount = recentGuestBookCount;
	}

	public int getFeedCount() {
		return feedCount;
	}

	public void setFeedCount(int feedCount) {
		this.feedCount = feedCount;
	}

	public String getHost() {
		return host;
	}

	public void setHost(String host) {
		this.host = host;
	}

	public int getRecentPostCount() {
		return recentPostCount;
	}

	public void setRecentPostCount(int recentPostCount) {
		this.recentPostCount = recentPostCount;
	}

	public String getMetaWeblogUserName() {
		return metaWeblogUserName;
	}

	public void setMetaWeblogUserName(String metaWeblogUserName) {
		this.metaWeblogUserName = metaWeblogUserName;
	}

	public String getMetaWeblogPassword() {
		return metaWeblogPassword;
	}

	public void setMetaWeblogPassword(String metaWeblogPassword) {
		this.metaWeblogPassword = metaWeblogPassword;
	}

	public String getPicasaUserName() {
		return picasaUserName;
	}

	public void setPicasaUserName(String googleAccount) {
		this.picasaUserName = googleAccount;
	}

	public String getGoogleReaderPassword() {
		return googleReaderPassword;
	}

	public void setGoogleReaderPassword(String password) {
		googleReaderPassword = password;
	}

	public String getGoogleReaderUserName() {
		return googleReaderUserName;
	}

	public void setGoogleReaderUserName(String userName) {
		this.googleReaderUserName = userName;
	}

	public String getPicasaAlbumName() {
		return picasaAlbumName;
	}

	public void setPicasaAlbumName(String picasaAlbumName) {
		this.picasaAlbumName = picasaAlbumName;
	}

	public String getErrorPage() {
		return errorPage;
	}

	public void setErrorPage(String errorPage) {
		this.errorPage = errorPage;
	}
	public String getHeadText() {
		return headText;
	}

	public void setHeadText(String headText) {
		this.headText = headText;
	}

	public String getBodyText() {
		return bodyText;
	}

	public void setBodyText(String bodyText) {
		this.bodyText = bodyText;
	}

	public String getFooterText() {
		return footerText;
	}

	public void setFooterText(String footerText) {
		this.footerText = footerText;
	}

	public String getPostText() {
		return postText;
	}

	public void setPostText(String postText) {
		this.postText = postText;
	}


	public void setUniqsite(boolean uniqsite) {
		this.uniqsite = uniqsite;
	}

	public boolean isUniqsite() {
		return uniqsite;
	}

	public int getFileNumPerPage() {
		return 50;
	}

	public int getFeedNumPerpage() {
		return 10;
	}

	public String getDoubanUserId() {
		return this.doubanUserId;
	}
	public void setDoubanUserId(String doubanUserId) {
		this.doubanUserId = doubanUserId;
	}

}